package com.shiliao.app.activity.interlocution.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shiliao.app.R;
import com.shiliao.app.activity.index.ui.InviteFriendDialog;
import com.shiliao.app.activity.interlocution.dialog.QuestionDialog;
import com.shiliao.app.activity.interlocution.presenter.CommonProblemDetailPresenter;
import com.shiliao.app.activity.interlocution.view.CommonProblemDetailView;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.weight.ActionBarView;
import com.shiliao.app.weight.ProgressWebView;
import com.yimei.framework.base.ui.BaseActivity;
import com.yimei.framework.util.LogUtil;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by yinzhiqiang on 2017/10/10.
 */

public class CommonProblemDetailActivity extends BaseActivity implements CommonProblemDetailView {

    @BindView(R.id.title_bar)
    ActionBarView titleBar;

    @BindView(R.id.webview)
    ProgressWebView webview;

    @BindView(R.id.ll_question)
    LinearLayout question;

    @BindView(R.id.index_comment)
    TextView comment;//评论内容

//    @BindView(R.id.tv_release_comment)
//    TextView releaseComment;//发布答案

    @BindView(R.id.iv_collection)
    ImageView collection;//收藏

    @BindView(R.id.iv_share)
    ImageView mShare;//分享

    private CommonProblemDetailPresenter commonProblemDetailPresenter;
    private Boolean showCollection = false;//判断收藏和取消收藏
    private String questionId;
    private String userId;
    private String loadUrl;
    private String isShow;

    private String shareTitle;//分享的title
    private String sharePicUrl;//分享的图标
    private String shareArticleUrl;//分享的链接
    private String shareDescription;//分享的描述
    private QuestionDialog questionDialog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_common_problem_detail;
    }

    @Override
    public void initView() {
        titleBar.setTitles("问题详情");
        titleBar.setBackClick(this);
        commonProblemDetailPresenter = new CommonProblemDetailPresenter(this);
        titleBar.getBackView().setVisibility(View.VISIBLE);
        questionDialog=new QuestionDialog(this);
        WebSetUtil.setWebView(webview);
        MyWebView myWebView = new MyWebView(webview);
        webview.setWebViewClient(myWebView);
        userId = YMUserManager.getUserId(this);
        Intent intent = getIntent();
        loadUrl = intent.getStringExtra("url");
        LogUtil.e("yzq", "====" + loadUrl);
        if (loadUrl.contains("question.php?id")) {//获取问题id
            questionId = loadUrl.substring(loadUrl.indexOf("?id=") + 4);
            if (questionId.contains("&"))
                questionId = questionId.substring(0, questionId.indexOf("&"));
        }
        commonProblemDetailPresenter.requestCheckQuestion(userId, questionId);//请求是否收藏问题接口
        commonProblemDetailPresenter.requestShareContent(questionId, userId);
        //设置 cookie, added by Luyi
        //String cookie;
        //cookie="current_user="+userId;
        //CookieManager cookieManager = CookieManager.getInstance();
        //cookieManager.setAcceptCookie(true);
        //cookieManager.acceptCookie();
        //cookieManager.removeAllCookies(true);
        //cookieManager.setCookie(YMServerUtil.getServerUrl(),cookie);
        //end of setcookie
        webview.loadUrl(loadUrl);
//        releaseComment.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {//提交回答
//                String commentContent = comment.getText().toString().trim();
//                if (TextUtils.isEmpty(commentContent)) {
//                    showToast("请输入评论内容");
//                    return;
//                }
//                commonProblemDetailPresenter.requestQuestionAnswer(userId, questionId, commentContent);
//            }
//        });

    }

    @OnClick(R.id.iv_collection)
    public void clickCollection() {//收藏按钮的点击事件
        if (!TextUtils.isEmpty(isShow) && isShow.equals("no")) {
            LogUtil.e("yzq", isShow);
            if (!showCollection) {
                LogUtil.e("yzq", "收藏");
                showCollection = !showCollection;
                commonProblemDetailPresenter.requestCollectionQuestion(userId, questionId);
            } else {
                LogUtil.e("yzq", "取消收藏");
                showCollection = !showCollection;
                commonProblemDetailPresenter.requestCancelQuestion(userId, questionId);
            }
        } else {
            if (showCollection) {
                LogUtil.e("yzq", "收藏");
                showCollection = !showCollection;
                commonProblemDetailPresenter.requestCollectionQuestion(userId, questionId);
            } else {
                LogUtil.e("yzq", "取消收藏");
                showCollection = !showCollection;
                commonProblemDetailPresenter.requestCancelQuestion(userId, questionId);
            }
        }
    }

    @OnClick(R.id.iv_share)
    public void toShare() {
//                        newsDetailPresenter.requestShare(article_id,userId);
        if (!TextUtils.isEmpty(shareArticleUrl)) {
            InviteFriendDialog dialog = new InviteFriendDialog(CommonProblemDetailActivity.this,
                    shareArticleUrl, shareTitle, sharePicUrl, shareDescription, userId,
                    "question", questionId);
            dialog.show();
        }

    }

    @OnClick(R.id.index_comment)
    public void comment(){
        questionDialog.setHintText("回答内容");
        questionDialog.setTitleText("请输入回答内容");
        questionDialog.show(false);
        questionDialog.showKeyBoard();
        questionDialog.setComfirn(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(questionDialog.getContent())) {
                    showToast("请输入回答内容");
                    return;
                }
                commonProblemDetailPresenter.requestQuestionAnswer(userId, questionId, questionDialog.getContent());
                questionDialog.dismiss();
            }
        });
    }

    public class MyWebView extends WebViewClient {
        public MyWebView(WebView webView) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            LogUtil.e("yzq", loadUrl);
            //webview.loadUrl(loadUrl);
            return false;
        }
    }

    @Override
    public void commentSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webview.reload();
                showToast("感谢您的回答");
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(comment.getWindowToken(), 0);
            }
        });
    }

    @Override
    public void isCollectionQuestion(final String isCollection) {//,是否收藏该问题
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isShow = isCollection;
                if (!TextUtils.isEmpty(isCollection) && "yes".equals(isCollection)) {
                    collection.setImageResource((R.mipmap.icon_collection_sel));
                }

            }
        });
    }

    @Override
    public void setCollectionIcon() {//收藏问题的图片
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                collection.setImageResource(R.mipmap.icon_collection_sel);
            }
        });
    }

    @Override
    public void setNoCollection() {//没有问题文章的图片
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                collection.setImageResource(R.mipmap.icon_collection);
            }
        });
    }

    @Override
    public void shareContent(String title, String picUrl, String articleUrl, String description) {
        shareTitle = title;
        shareArticleUrl = articleUrl;
        shareDescription = description;
        sharePicUrl = picUrl;
    }
}
